package com.ny.view;

import com.ny.dao.ManagerDao;
import com.ny.dao.impl.ManagerDaoImpl;
import com.ny.entity.Manager;
import com.ny.entity.Student;
import com.ny.service.StudentService;
import com.ny.service.impl.StudentServiceImpl;

import java.sql.SQLException;
import java.util.Scanner;
//调用业务层的方法
public class ViewMain {

    private static StudentService service ;

    public static <AdminView> void main(String[] args) throws SQLException, ClassNotFoundException {

        service = new StudentServiceImpl();

        ManagerDao managerDao = new ManagerDaoImpl() {
            @Override
            public Manager getAdminByCredentials(String adminName, String adminPassword) {
                return null;
            }
        };

        Student student = new Student();


        boolean flag = true;
        while (flag){
            System.out.println("欢迎来到校园商品售卖管理系统");
            System.out.println("请选择你的操作");
            System.out.println("1.用户登录");
            System.out.println("2.管理员登录");
            System.out.println("3.注册账号");
            System.out.println("4.修改密码"); // 新增选项
            System.out.println("0.退出系统");

            Scanner scanner  = new Scanner(System.in);
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    System.out.println("用户登录");
                    System.out.println("请输入用户名");
                    String studentName = scanner.next();
                    System.out.println("请输入密码");
                    String studentPassword = scanner.next();
                    // 调用业务层的登录功能：
                    student = service.login(studentName, studentPassword);
                    if(student!=null){
                        System.out.println("登录成功");
                        IndexView indexView = new IndexView();
                        indexView.indexView(student);
                    }
                    else {System.out.println("登录失败");}
                    break;

                case 2:
                    System.out.println("管理员登录");
                    System.out.println("请输入管理员用户名");
                    String adminName = scanner.next();
                    System.out.println("请输入管理员密码");
                    String adminPassword = scanner.next();
                    Manager manager = managerDao.getAdminByCredentials(adminName, adminPassword);
                    if(manager !=null){
                        System.out.println("登录成功");
                        IndexView indexView = new IndexView();
                        indexView.indexView(student);

                    } else {System.out.println("登录失败");}
                    break;
                    
                case 3:
                    System.out.println("请输入用户名");
                    student.setStudentName(scanner.next());
                    System.out.println("请输入密码");
                    student.setStudentPassword(scanner.next());
                    System.out.println("请输入手机号");
                    student.setStudentPhone(scanner.next());
                    int add = service.add(student);
                    break;
                case 4:
                    System.out.println("修改密码");
                    System.out.println("请输入用户名");
                    String username = scanner.next();
                    System.out.println("请输入旧密码");
                    String oldPassword = scanner.next();
                    System.out.println("请输入新密码");
                    String newPassword = scanner.next();

                    try {
                        int result = service.updatePassword(username, oldPassword, newPassword);
                        if (result > 0) {
                            System.out.println("密码修改成功！");
                        } else {
                            System.out.println("原密码错误或修改失败，请重试。");
                        }
                    } catch (SQLException e) {
                        System.out.println("数据库异常：" + e.getMessage());
                    }
                    break;
                case 5:
                    flag = false;
                    break;

            }
        }



    }



}
